
#include <boost/test/unit_test.hpp>


#define _DEBUGING
#include "../AllocatorHeapBased.h"




BOOST_AUTO_TEST_CASE(TestAllocatorHeapBased_1)
{
    static const unsigned int _byteAlignment=sizeof(int);
    static const unsigned int _howManyAllocated=256;
    static const unsigned int _loops=100;

    AllocatorHeapBased alloc(_howManyAllocated*sizeof(int),_howManyAllocated,_byteAlignment);


        for(int i=0;i<_loops;i++)
        {
            for(int sizeToAllocate=1;sizeToAllocate<=_howManyAllocated;sizeToAllocate*=2)
            {
                void* pTab[_howManyAllocated+1];
                for(int j=sizeToAllocate;j<=_howManyAllocated;j+=sizeToAllocate)
                {
                    pTab[j]=alloc.allocate(sizeToAllocate*sizeof(int));
                }

                for(int j=sizeToAllocate;j<=_howManyAllocated;j+=sizeToAllocate)
                {
                    alloc.deallocate(pTab[j]);
                }
            }
        }

}


BOOST_AUTO_TEST_CASE(TestAllocatorHeapBased_2)
{

    static const unsigned int _byteAlignment=sizeof(int);
    static const unsigned int _howManyAllocated=256;
    static const unsigned int _loops=100;

    AllocatorHeapBased alloc(_howManyAllocated*sizeof(int),_howManyAllocated,_byteAlignment);


        for(int i=0;i<_loops;i++)
        {
            int* pTab[_howManyAllocated];

            for(int j=0;j<_howManyAllocated;j++)
            {
                pTab[j]=(int*)alloc.allocate(sizeof(int));
                *pTab[j]=j;
            }

            static const int divide=64;

            for(int j=_howManyAllocated-1;j>=divide;j--)
            {
                BOOST_CHECK(*pTab[j]==j);
                alloc.deallocate(pTab[j]);
            }

            for(int j=0;j<divide;j+=2)
            {
                BOOST_CHECK(*pTab[j]==j);
                alloc.deallocate(pTab[j]);
            }

            for(int j=1;j<divide;j+=2)
            {
                BOOST_CHECK(*pTab[j]==j);
                alloc.deallocate(pTab[j]);
            }

        }

}


BOOST_AUTO_TEST_CASE(TestAllocatorHeapBased_3)
{
    static const unsigned int _byteAlignment=sizeof(int);
    static const unsigned int _howManyAllocated=10;


    AllocatorHeapBased alloc(_howManyAllocated*sizeof(int),_howManyAllocated,_byteAlignment);

    void* pointer=alloc.allocate(sizeof(int));
    alloc.deallocate(pointer);
    BOOST_CHECK_THROW(alloc.deallocate(pointer),std::exception);

    alloc.allocate(sizeof(int));
    BOOST_CHECK_THROW(for(int i=0;i<10*_howManyAllocated;i++)alloc.allocate(sizeof(int)),std::exception);
}



BOOST_AUTO_TEST_CASE(TestAllocatorHeapBased_4)
{
    static const unsigned int _byteAlignment=4;
    static const unsigned int _howManyAllocated=10;


    AllocatorHeapBased alloc(_howManyAllocated*sizeof(int),_howManyAllocated,_byteAlignment);

    for(int i=0;i<_howManyAllocated;i++)
    {
        void* pointer=alloc.allocate(sizeof(int));
        BOOST_CHECK(pointer==ALLIGNUP(pointer,_byteAlignment));
    }

}



BOOST_AUTO_TEST_CASE(TestAllocatorHeapBased_5)
{
    static const unsigned int _byteAlignment=64;
    static const unsigned int _howManyAllocated=10;


    AllocatorHeapBased alloc(_howManyAllocated*sizeof(int),_howManyAllocated,_byteAlignment);

    for(int i=0;i<_howManyAllocated;i++)
    {
        void* pointer=alloc.allocate(sizeof(int));
        BOOST_CHECK(pointer==ALLIGNUP(pointer,_byteAlignment));
    }

}


BOOST_AUTO_TEST_CASE(TestAllocatorHeapBased_6)
{
    static const unsigned int _byteAlignment=8;
    static const unsigned int _howManyAllocated=1;


    AllocatorHeapBased alloc(_howManyAllocated*sizeof(int),_howManyAllocated,_byteAlignment);

    for(int i=0;i<_howManyAllocated;i++)
    {
        void* pointer=alloc.allocate(sizeof(int));
        BOOST_CHECK(pointer==ALLIGNUP(pointer,_byteAlignment));
    }

}
